<?php

// start benchmarking
$__pageload_start = microtime(true);

$base_domain = '127.0.0.1';
$cookie_domain = '127.0.0.1';
$base_url = "http://{$base_domain}/";
$secure_base_url = "https://{$base_domain}/";

$default_page = 'index';
$default_theme = 'default';
$meta_title = '';
$html_title = '';
$meta_description = '';
$meta_keywords = '';

$general_version = 1;

$session_name = 'ape-attack-session';
$atoken_name = 'ape-auth-token';

// database connection values
$db_default_host = 'localhost';
$db_default_user = 'apeattack';
$db_default_pass = 'apeattack';
$db_default_db = 'apeattack';

// define a debug variable to dump lines into
$debug_string = '';

// bookmarks host
$bookmarks_host = 'www.svidgen.com';

// use zlib on-the-fly compression
// ini_set('zlib.output_compression', '1');

// level between 1 and 9 or -1 to let server decide
// ini_set('zlib.output_compression_level', '-1');

// turn off output handlers
// ini_set('zlib.output_handler', '');


// now, if site_config.inc exists, load it up ...
if (file_exists('site_config.inc')) {
	if (!@include('site_config.inc')) {
		// that's OK ... 
	}
}


// check for highly non-compliant browsers (IE < 7)
if (preg_match("/MSIE 6|5/", $_SERVER['HTTP_USER_AGENT'])) {
	$old_browser = true;
} else {
	$old_browser = false;
}


// fix GET and POST data for MySQL queries
if (get_magic_quotes_gpc()) {
	// print "<!-- magic quotes is on -->\n";
	
	// magic quotes is OFF! run escaping on GET, POST, and COOKIE variables
	foreach ($_GET as $key => $value) {
		$_GET[$key] = stripslashes($value);
	}

	foreach ($_POST as $key => $value) {
		$_POST[$key] = stripslashes($value);
	}
	
	foreach ($_COOKIE as $key => $value) {
		$_COOKIE[$key] = stripslashes($value);
	}
}


// start (or attach to) the session 
$session_duration = (int)ini_get("session.gc_maxlifetime");
session_set_cookie_params($session_duration, '/', $cookie_domain);
session_name($session_name);
session_start();


// if the .project_stop file exists, the project has been stopped or paused
// for some reason. it is the responsibility of .project_stop to actually
// stop or filter pages.
if (is_file('.project_stop')) {
	include('.project_stop');
}


// buffer the output of the page file, so it can interact with the template
// and send browser headers
ob_start();

// if a page is designate on in the query string, use it.  otherwise, use the
// default page name
if (!isset($_GET['driver_c'])) {
	$_GET['driver_c'] = $default_page;
}


// pages will use a .inc extension
// also, remove .'s from the pagename so parent directories
// cannot be accessed.
$template_content = 'pages/' . str_replace('.', '', $_GET['driver_c']) . '.inc';

unset($_GET['driver_c']);

// if the page name is not a file or cannot be included for some reason,
// use the broken fingers message for page data
if ((!is_file($template_content)) || (!include($template_content))) {
	header('HTTP/1.1 404 Not Found');
	$meta_title = "404 - Not Found";
	print "
		Whatever the hell you're looking for ain't here!
	";
}

// grab output buffered contents and stop buffering
$template_page_data = ob_get_contents();
ob_end_clean();

// if an html_title has not been set, us the meta_title
if (!$html_title) {
	$html_title = $meta_title;
}

// use output buffering again for the template, in case it needs to modify
// headers in any way
ob_start();

// if a theme is designated in the query string, use it.  otherwise, use the
// default template 

// if there is a preferred theme, use that
if (isset($prefer_theme) && $prefer_theme) {
	$c_theme = $prefer_theme;
}

// however, if a theme has been specified, use that
if (isset($_GET['driver_theme']) && $_GET['driver_theme']) {	
	$c_theme = $_GET['driver_theme'];
}

// and lastly, if there is a required theme, use that instead
if (isset($require_theme) && $require_theme) {
	$c_theme = $require_theme;
}

// now, check and fix the theme value
if (!isset($c_theme)) {
	$c_theme = $default_theme;
}

// templates will use a .template extension
$template_file = 'themes/' . str_replace('.', '', $c_theme) . '.inc';
// $template_file = $_GET['theme'] . '.inc';


// if the template doesn't exist or cannot be included for some reason,
// use a very bare-bones template
if ((!is_file($template_file)) || (!include($template_file))) {

print '<!doctype html>
<html>
<head>
	<title>' . $meta_title . '</title>
	<meta name="keywords" content="' . $meta_keywords . '" />
	<meta name="description" content="' . $meta_description . '" />
</head>
<body>' . $template_page_data . '</body>
</html>';

}


// reset/extend the SESSION cookie if already set
if (isset($_COOKIE[$session_name])) {
	setcookie($session_name, $_COOKIE[$session_name], time() + $session_duration, "/", $cookie_domain);
}


// end buffering and flush all output to the client
ob_end_flush();




// commonly used fn()'s ...


function current_timestring() {
	if (isset($_SESSION['tz_offset']) && $_SESSION['tz_offset']) {
		$offset = $_SESSION['tz_offset'];
	} else {
		$offset = 300;
	}

	return gmdate('D, d M Y g:i A (\G\M\T' . offset_string($offset) . ')', time() - round($offset * 60));
} // current_timestring()


function offset_string($o) {
	if ($o == 0) {
		return '';
	} elseif ($o > 0) {
		$sign = '-';
	} else {
		$sign = '+';
	}

	$m = $o % 60;
	$h = ($o - $m) / 60;

	$m = str_pad($m, 2, '0', STR_PAD_LEFT);
	$h = str_pad($h, 2, '0', STR_PAD_LEFT);

	return $sign . $h . ':' . $m;
} // offset_string()


function send_cache_control_headers($t = '+1 year') {
	// set cache-control header
	$cc_header = strtotime($t, 0);
	header("Cache-Control: max-age={$cc_header}");

	// set expires header
	$exp_header = gmdate('D, d M Y H:i:s \G\M\T', strtotime($t));
	header("Expires: {$exp_header}");

	// set pragma header to NOT "no-cache"
	// to avoid stepping on any pages' toes that might wish to
	// use the pragma header, verify that it is set to no-cache
	// before overwrting.
	$current_headers = headers_list();
	foreach ($current_headers as $header) {
		if ($header == 'Pragma: no-cache') {
			header('Pragma: cache');
			break;
		}
	}
	
} // expires_headers()


// send redirect headers and HTML (for browsers that don't support Location header?)
function redirect($url) {

	header("Location: {$url}");

	print "<html>
		<head>
			<title>Page moved</title>
		</head>
		<body>
			This page has moved to <a href='{$url}'>{$url}</a>.
		</body>
		</html>
	";

	exit();
} // redirect()


function time_elapsed() {
	global $__pageload_start;
	return round(microtime(true) - $__pageload_start, 3);
} // time_elapsed();


?>
